<?php
/**
 * Created by PhpStorm.
 * User: Charles
 * Date: 2017/10/21
 * Time: 4:56
 */

namespace app\Http\Controllers\Wechat;


use App\Http\Controllers\Controller;
use App\Models\MyWechatUpload;
use App\Models\WechatUpload;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Session;

class ViewController extends Controller
{
    public function anyIndex(Request $request)
    {
        $openid = Session::get('wechat_openid');
        $this->validate($request, ['id' => 'required|int']);
        $wechatUpload = WechatUpload::findOrFail($request->id);
        if (0 != $wechatUpload->status) {
            abort(404);
        }
        if ($wechatUpload->openid != $openid) {
            $myWechatUpload = MyWechatUpload::query()
                ->where('openid', $openid)
                ->where('wechat_upload_id' ,$request->id)
                ->first();
            if (null === $myWechatUpload) {
                return redirect('/wechat/payment/pay?id=' . $request->id);
            }
        }
        return view('wechat.view', compact('wechatUpload', 'openid'));
    }
}